<template>
	<view>
		<uni-forms class="myForm" ref="myForm" :modelValue="memorabilia" :rules="rules">
			<uni-forms-item name="content">
				<label>大事件内容：</label>
				<uni-easyinput class="input" type="textarea" autoHeight v-model="memorabilia.content" placeholder="请输入内容"/>
			</uni-forms-item>
			<uni-forms-item name="happenTime">
				<label>大事件发生时间：</label>
				<uni-easyinput class="input" type="datetime-local" v-model="memorabilia.happenTime" />
			</uni-forms-item>
			<button type="primary" @click="addMemorabilia">添加</button>
		</uni-forms>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue';
	import {postRequest} from '@/http'
	import useUserStore from '@/store/user.js'
	const userStore = useUserStore();
	const myForm = ref(null); 
	//大事件
	const memorabilia = reactive({
		content: '',
		happenTime: '',
		userId:userStore.userinfo.id //登录的用户ID
	})
	//表单验证规则
	const rules = {
		content: {
			rules: [{
				required: true,
				errorMessage: '请输入相关事件内容'
			}]
		},
		happenTime: {
			rules: [{
				required: true,
				errorMessage: '请选择事件发生的时间'
			}]
		}
	}
	//添加大事件
	const addMemorabilia = () => {
		myForm.value.validate().then(res => {
			postRequest('api/memorabilia/add',memorabilia).then(res=>{
			 console.log(res)
				if(res.success){
					uni.showToast({  
					  title: '添加成功',  
					  icon: 'success',  
					  duration: 2000,  
					  success: function () {  
					    uni.switchTab({  
					      url: '/pages/index/bigevent/bigevent'
					    });  
					  }  
					});
				}
			}).catch(err=>{
				console.log(err)
			})

		}).catch(err => {
			console.log(err)
		})
	}
	
</script>

<style scoped>

.myForm{
  margin: 20rpx;
}


</style>